<template>
  <div id="contain">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span class="title">{{ infoList.name }}</span>
        <el-button type="text" style="float: right; padding: 10px 5px; font-size: 18px" @click="back2Home">返回首页</el-button>
      </div>
      <div style="padding: 10px 0 25px 10px">
        <div>
          <img :src="infoList.pic" alt="图片加载失败" class="imgbox" />
        </div>
        <div class="intro">
          <p style="margin: 10px 0">
            <span class="intospan">起点：</span>{{ infoList.startAddr }}
          </p>
          <p style="margin: 10px 0">
            <span class="intospan">终点：</span>{{ infoList.endAddr }}
          </p>
          <p style="margin: 10px 0">
            <span class="intospan">线路：</span>{{ infoList.route }}
          </p>
          <p style="margin: 10px 0">
            <span class="intospan">天数:</span>{{ infoList.days }}
          </p>
          <p style="margin: 10px 0">
            <span class="intospan">类型：</span
            >{{
              infoList.type === 1
                ? "跟团游"
                : infoList.type === 2
                ? "私团"
                : infoList.type === 3
                ? "自助"
                : "半自助"
            }}
          </p>
          <p style="margin: 10px 0">
            <span class="intospan">成人价：</span>{{ infoList.adPrice }}
          </p>
          <p style="margin: 10px 0">
            <span class="intospan">儿童价：</span>{{ infoList.chPrice }}
          </p>
        </div>
        <el-button type="primary" @click="btnclick">报名跟团游</el-button>
      </div>
      <el-dialog :visible.sync="dialogFormVisible">
        <div v-if="dialogindex === 1">
          <p style="color: orange">报名</p>
          <el-form :model="infoList">
            <el-form-item label="服务名称" :label-width="formLabelWidth">
              <el-input v-model="infoList.name" disabled></el-input>
            </el-form-item>
            <el-form-item label="服务线路" :label-width="formLabelWidth">
              <el-input v-model="infoList.route" disabled></el-input>
            </el-form-item>
            <el-form-item label="服务起点" :label-width="formLabelWidth">
              <el-input v-model="infoList.startAddr" disabled></el-input>
            </el-form-item>
            <el-form-item label="服务终点" :label-width="formLabelWidth">
              <el-input v-model="infoList.endAddr" disabled></el-input>
            </el-form-item>
            <el-form-item label="儿童数量" :label-width="formLabelWidth">
              <el-input-number
                v-model="chNum"
                :min="0"
                :max="10"
              ></el-input-number>
            </el-form-item>
            <el-form-item label="成人数量" :label-width="formLabelWidth">
              <el-input-number
                v-model="adNum"
                :min="0"
                :max="10"
              ></el-input-number>
            </el-form-item>
            <el-form-item>
              <el-button
                @click="dialogFormVisible = false"
                style="float: right; margin: 0 15px"
                >取 消</el-button
              >
              <el-button type="primary" @click="submit1" style="float: right"
                >确 定</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div v-if="dialogindex === 2">
          <p style="color: orange">订单详情</p>
          <el-table :data="chaxunlist" style="width: 1000px" stripe>
            <el-table-column prop="name" label="线路名" align="center">
            </el-table-column>
            <el-table-column
              prop="createTime"
              label="创建时间"
              align="center"
            ></el-table-column>
            <el-table-column prop="adPrice" label="成人价格" align="center">
            </el-table-column>
            <el-table-column prop="adNum" label="成人数量" align="center">
            </el-table-column>
            <el-table-column prop="chPrice" label="儿童价格" align="center">
            </el-table-column>
            <el-table-column prop="chNum" label="儿童数量" align="center">
            </el-table-column>
            <el-table-column prop="pay" label="总价" align="center">
            </el-table-column>
          </el-table>
          <div style="height: 80px">
            <div style="float: right; margin: 40px 10px">
              <el-button @click="xiugai">修改</el-button>
              <el-button @click="quxiao" style="margin: 0 20px"
                >取消订单</el-button
              >
              <el-button type="primary" @click="zhifu">去支付</el-button>
            </div>
          </div>
        </div>
        <div v-if="dialogindex === 3">
          <p style="color: orange">修改</p>
          <el-form :model="infoList">
            <el-form-item label="服务名称" :label-width="formLabelWidth">
              <el-input v-model="infoList.name" disabled></el-input>
            </el-form-item>
            <el-form-item label="服务线路" :label-width="formLabelWidth">
              <el-input v-model="infoList.route" disabled></el-input>
            </el-form-item>
            <el-form-item label="服务起点" :label-width="formLabelWidth">
              <el-input v-model="infoList.startAddr" disabled></el-input>
            </el-form-item>
            <el-form-item label="服务终点" :label-width="formLabelWidth">
              <el-input v-model="infoList.endAddr" disabled></el-input>
            </el-form-item>
            <el-form-item label="儿童数量" :label-width="formLabelWidth">
              <el-input-number
                v-model="chNum"
                :min="0"
                :max="10"
              ></el-input-number>
            </el-form-item>
            <el-form-item label="成人数量" :label-width="formLabelWidth">
              <el-input-number
                v-model="adNum"
                :min="0"
                :max="10"
              ></el-input-number>
            </el-form-item>
            <el-form-item>
              <el-button
                style="float: right; margin: 0 15px"
                @click="quxiaoback"
                >取 消</el-button
              >
              <el-button type="primary" @click="submit2" style="float: right"
                >确 定</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </el-dialog>
    </el-card>
    <el-card>
      <div slot="header" class="clearfix">
        <span class="title2">驴友评价</span>
      </div>
      <div>
        <div v-for="(item, index) in pingjiaList" :key="index" class="forbox1">
          <img :src="item.headUrl" alt="" style="width: 100px; height: 100px" />
          <span style="color: orange">{{ item.username }}</span>
          <p style="color: #1be25d">{{ satisfaction[item.satisfaction ]}}</p>
          <p>{{ item.content }}</p>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      token: window.localStorage.getItem("token"),
      goodsId: this.$route.query.goodsId,
      infoList: {},
      dialogFormVisible: false,
      formLabelWidth: "120px",
      chNum: 0,
      adNum: 0,
      orderId: "",
      dialogindex: 1,
      goPay: false,
      chaxunlist: [],
      satisfaction:['不满意','一般','满意','非常满意'],
      pingjiaList: [

      ],
    };
  },
  beforeRouteLeave(to, from, next) {
    console.log(to);
    if (this.goPay) {
      localStorage.setItem("query", JSON.stringify(from.query));
      localStorage.setItem("path", from.path);

      next();
    } else {
      next();
    }
  },
  mounted() {
    this.getInfo();
    this.getComment()
  },
  methods: {
    back2Home () {
      if (this.$route.query.fromwhere) {
        this.$router.push({ path: '/', query: {activeIndex: '0'}})
        return
      }
      this.$router.push({ path: '/', query: {activeIndex: '2'}})
    },
    getInfo() {
      this.$axios({
        method: "post",
        url: "/static/goods/info",
        data: { goodsId: this.goodsId },
      }).then((res) => {
        this.infoList = res.data.page;
      });
    },
    btnclick() {
      if (!this.token) {
        this.$message.error("请先登录");
        return false;
      }
      this.dialogFormVisible = true;
    },
    getComment() {
      this.$axios.post('/review/list',{
        goodsId:this.goodsId
      }).then(res => {
        if(res.data.code === 0) {
          this.pingjiaList = res.data.page;
        }
      })
    },
    submit1() {
      if (this.token) {
        this.$axios({
          method: "post",
          url: "/order/create",
          headers: { token: this.token },
          data: {
            goodsId: this.goodsId,
            chNum: this.chNum,
            adNum: this.adNum,
            merchantId: this.infoList.userId,
          },
        }).then((res) => {
          console.log("创建订单返回信息");
          console.log(res);
          if (res.data.code === 500) {
            this.dialogFormVisible = false;
            this.$message({
              type: "error",
              message: res.data.msg,
            });
          }
          if (res.data.code === 0) {
            this.orderId = res.data.page.orderId;
            this.dialogindex = 2;
            this.chakan1();
          }
        });
      }
    },
    chakan1() {
      this.chaxunlist = [];
      this.$axios({
        method: "post",
        url: "/order/info",
        headers: { token: this.token },
        data: { orderId: this.orderId + "" },
      }).then((res) => {
        // console.log("chaxun");
        // console.log(res);
        let goods = res.data.goods;
        let page = res.data.page;
        if (res.data.code === 0) {
          let form = {};
          form.name = goods.name;
          form.adPrice = goods.adPrice;
          form.chPrice = goods.chPrice;

          form.createTime = page.createTime;
          form.adNum = page.adNum;
          form.chNum = page.chNum;
          form.pay = page.pay;
          this.chaxunlist.push(form);
        }
      });
    },
    xiugai() {
      this.dialogindex = 3;
    },
    quxiao() {
      this.$axios({
        method: "post",
        url: "/order/cancel",
        headers: { token: this.token },
        data: { orderId: this.orderId + "" },
      }).then((res) => {
        // console.log(res);
        if (res.data.code === 0) {
          this.$message({
            type: "success",
            message: "已取消！",
          });
          this.dialogFormVisible = false;
          this.dialogindex = 1;
        }
      });
    },
    zhifu() {
      let data = {
        out_trade_no: this.orderId + "",
        subject: this.chaxunlist[0].name,
        total_amount: this.chaxunlist[0].pay,
      };
      this.goPay = true;
      this.$router.push({ path: "zhifu", query: data });
    },
    quxiaoback() {
      this.dialogindex = 2;
      this.chakan1();
    },
    submit2() {
      this.$axios({
        method: "post",
        url: "/order/edit",
        headers: { token: this.token },
        data: {
          orderId: this.orderId + "",
          chNum: this.chNum,
          adNum: this.adNum,
        },
      }).then((res) => {
        if (res.data.code === 500) {
          this.dialogFormVisible = false;
          this.$message({
            type: "error",
            message: res.data.msg,
          });
        }
        if (res.data.code === 0) {
          this.chakan1();
          this.dialogindex = 2;
        }
      });
    },
  },
};
</script>
<style scoped>
#contain {
  width: 1200px;
  margin: 0 auto;
  padding-top: 10px;
}
.imgbox {
  width: 500px;
  height: 300px;
}
.clearfix {
  height: 40px;
}
.title {
  font-size: 28px;
  color: orange;
}
.intro {
  width: 1000px;
}
.intospan {
  margin-left: 5px;
  color: orange;
  font-size: 18px;
}
.title2 {
  font-size: 22px;
  color: rgb(87, 231, 135);
}
.forbox1 {
  margin: 20px 10px;
  border-bottom: 1px solid #ccc;
  padding-bottom: 10px;
}
.forbox1 img {
  border-radius: 50%;
}
.forbox1 p {
  margin: 10px;
}</style
>>
